using System.Threading.Tasks;
using Newtonsoft.Json;
using Paradogs2.api;
using Paradogs2;
using UnityEngine;

namespace Paradogs2
{
    public class ServerMgr
    {
        public static async Task fastLogin(string account, string password)
        {
            Debug.Log(">>>> 请求登录服");
            var loginVO = await AccountRequest.login(account, password);
            Debug.Log("<<<< 登录成功");

            Debug.Log(">>>> 尝试获取服务器列表");
            var serverList = await ServerRequest.list(loginVO.token);
            Debug.Log("<<<< 获取服务器列表成功");


            var server = serverList[0];
            var ws = new WSGameServerConnection($"ws://{server.host}:{server.port}/gs");
            Debug.Log($">>>> 尝试连接游戏服：{JsonConvert.SerializeObject(server)}");
            bool connectResult = await ws.Connect();
            if (connectResult)
            {
                Debug.Log($"<<<< 连接游戏服成功");
            }
            else
            {
                Debug.Log($"<<<< 连接游戏服失败");
            }

            Debug.Log($">>>> 尝试登录游戏服");
            await NetMgr.RoleHandler.login(loginVO.token);
            Debug.Log($"<<<< 登录游戏服成功");
        }
    }
}